package com.aier.school.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolClassesManageAction")
public class SchoolClassesManageAction extends SchoolBaseAction{

    /**
     * 学校管理-班级管理
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classService")
    private IClassService classService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;    
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private Integer sid;        //学校id
    private Long tid;            //教师id
    private List<SchoolTeacher> teacherList;    //教师列表
    private List<Classes> classList;    //班级列表
    private List<Classes> schoolClassList;    //学校班级列表(供下拉选择)
    private Long cid;            //班级id
    private String cname;        //班级名称
    private String desinfo;        //班级信息
    private String manifesto;    //班级宣言
    private String teacheres;
    
    
    private int page;            // 页数
    private int page_size;        // 每页的记录数
    private int page_count;        // 总页数
    private int count;            // 总数
    
    
    @Override
    public String execute(){
        schoolClassList = classService.searchClassList(sid);
        teacherList = schoolTeacherService.selectTeacher(sid,null,null);
        
        return SUCCESS;
    }
    
    /**
     * 查询学校班级
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchClassList(){
        s = 1;
        Map<String,Object> map = classService.searchClassList(page, SchoolFinalPageSize.CLASSES_MAINTAIN_LIST_PAGESIZE, sid,cid);
        if(null==map){
            s = 0;
        }
        classList = (List<Classes>) map.get(ICommonDAO.OBJ_LIST);
        if (null == classList) {
            s = 0;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 删除班级
     * @return
     */
    public String deleteClass(){
        s = classService.deleteClass(cid);
        return SUCCESS;
    }
    
    /**
     * 添加班级
     * @return
     */
    public String addClass(){
        Timestamp ctime = new Timestamp(new Date().getTime());
        int class_num = classService.countClassByName(cname, sid, null);
        System.out.println(teacheres);
        
        if(class_num>0){
            s = 0;
            error_info = "班级名已经存在";            
        }else{
            Classes classes = new Classes(null, cname, sid, desinfo, manifesto, null, null, ctime);
            s = classService.insertClass(classes,teacheres); 
            
            System.out.println(s);
        }
        return SUCCESS;
    }

    
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<SchoolTeacher> getTeacherList() {
        return teacherList;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public void setDesinfo(String desinfo) {
        this.desinfo = desinfo;
    }

    public void setManifesto(String manifesto) {
        this.manifesto = manifesto;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setTeacheres(String teacheres) {
        this.teacheres = teacheres;
    }

    public List<Classes> getSchoolClassList() {
        return schoolClassList;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

}
